<?php

class GmToolsNoticeService
{

    /**
     * @var GmToolsNoticeService
     */
    private static $instance;

    /**
     * 单例函数
     * @return GmToolsNoticeService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new GmToolsNoticeService();
        }
        return self::$instance;
    }

    /**
     * VisitInitService constructor.
     */
    private function __construct()
    {

    }

    public function create($language, $startTime, $endTime, $title, $content, $sort, $channel, $picture)
    {
        $_isSuccess = true;
        $_noticeId = 0;
        /** @var GmSaveNoticeVoList $_gmSaveNoticeVoList */
        $_gmSaveNoticeVoList = SzPersister::get()->getVoList(0, GameConst::ORM_NAME_GM_TOOLS_NOTICE);
        /** GmSaveNoticeVo $_gmSaveNoticeVo */
        $_gmSaveNoticeVo = new GmSaveNoticeVo(null, 0, $language, $startTime, $endTime, $title, $content, $sort, $channel, $picture, 1, true);
        /** @var SzMySqlModel $_gmSaveNoticeVoModel */
        $_gmSaveNoticeVoModel = SzPersister::get()->getModel(GameConst::ORM_NAME_GM_TOOLS_NOTICE);
        list($affectedRows, $noticeId) = $_gmSaveNoticeVoModel->insert($_gmSaveNoticeVo);
        if ($affectedRows) {
            $_gmSaveNoticeVo->setNoticeId($noticeId);
            $_gmSaveNoticeVo->removeInsertFlag();
            $_gmSaveNoticeVoList->setElement($noticeId, $_gmSaveNoticeVo);
            $_gmSaveNoticeVoModel->setListOfVoCache(array($_gmSaveNoticeVo), $_gmSaveNoticeVoList->getList());
            $_noticeId = $noticeId;
            SzPersister::get()->setVoList($_gmSaveNoticeVoList);
        } else {
            $_isSuccess = false;
        }
        return array($_isSuccess, $_noticeId);
    }

    public function changeNotice($noticeId, $language, $startTime, $endTime, $title, $content, $Sort, $Channel, $Picture)
    {
        /** @var GmSaveNoticeVoList $_gmSaveNoticeVoList */
        $_gmSaveNoticeVoList = SzPersister::get()->getVoList(0, GameConst::ORM_NAME_GM_TOOLS_NOTICE);
        /** @var GmSaveNoticeVo $_gmSaveNoticeVo */
        $_gmSaveNoticeVo = $_gmSaveNoticeVoList->getElement($noticeId, false);
        if (!$_gmSaveNoticeVo) {
            return false;
        }
        $_gmSaveNoticeVo->setLanguage($language);
        $_gmSaveNoticeVo->setStartTime($startTime);
        $_gmSaveNoticeVo->setEndTime($endTime);
        $_gmSaveNoticeVo->setTitle($title);
        $_gmSaveNoticeVo->setContent($content);
        $_gmSaveNoticeVo->setSort($Sort);
        $_gmSaveNoticeVo->setChannel($Channel);
        $_gmSaveNoticeVo->setPicture($Picture);
        $_gmSaveNoticeVoList->updateElement($noticeId, $_gmSaveNoticeVo);
        SzPersister::get()->setVoList($_gmSaveNoticeVoList);

        return true;
    }

    public function deleteNotice($noticeId)
    {
        /** @var GmSaveNoticeVoList $_gmSaveNoticeVoList */
        $_gmSaveNoticeVoList = SzPersister::get()->getVoList(0, GameConst::ORM_NAME_GM_TOOLS_NOTICE);
        /** @var GmSaveNoticeVo $_gmSaveNoticeVo */
        $_gmSaveNoticeVo = $_gmSaveNoticeVoList->getElement($noticeId, false);
        if (!$_gmSaveNoticeVo) {
            return false;
        }
        $_gmSaveNoticeVo->setIsValid(0);
        $_gmSaveNoticeVoList->updateElement($noticeId, $_gmSaveNoticeVo);
        SzPersister::get()->setVoList($_gmSaveNoticeVoList);

        return true;
    }

    public function getNotice($noticeId)
    {
        return array(false, null);
    }

    public function getNoticeList($userId)
    {
        return [];
    }
}